home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
SNIP9_91.ARJ
/
HEAD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-03
|
1KB
|
43 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUL '\000'
#define BEL '\007'
#define LINE_LEN 132
give_up(msg)
char *msg; /* This is for Unix so use non-ANSI definitions */
{
putchar(BEL);
puts(msg);
exit(-1);
}
main(argc, argv)
int argc;
char *argv[];
{
FILE *infile;
char line[LINE_LEN + 2]; /* Allow for '\n' & NUL */
int i, N = 0;
if (2 > argc)
give_up("Usage: HEAD file [number_of_lines]");
if (NULL == (infile = fopen(argv[1], "r")))
give_up("Unable to open input file");
if (2 < argc)
N = atoi(argv[2]);
if (!N) N = 4;
for (i = 0; i < N; ++i)
{
if (NULL == fgets(line, LINE_LEN + 1, infile))
break;
line[LINE_LEN + 1] = NUL; /* Allow too-long lines */
fputs(line, stdout);
if (!strrchr(line, '\n'))
i -= 1; /* More to read */
}
fclose(infile);
}